//
//  PlayBlueMusicListManager.m
//  singleM-BOX
//
//  Created by xiaomei su on 2018/1/15.
//  Copyright © 2018年 xuweixin. All rights reserved.
//

#import "PlayBlueMusicListManager.h"

@implementation PlayBlueMusicListManager
-(NSArray *)getSongNameFromBlueWithArray:(NSArray *)dataArray{
    //拼接歌名
    NSMutableString *tempString = [NSMutableString string];
    NSMutableArray *tempArray = [NSMutableArray array];
    for (NSString *string in dataArray) {
        NSString *status = [string substringWithRange:NSMakeRange(6, 2)];
        if ([status isEqualToString:@"00"]) {
            [tempString appendString:[string substringFromIndex:8]];
            if ([self StringFomHexString:tempString].length > 0) {
                [tempArray addObject:[self StringFomHexString:tempString]];
                [tempString deleteCharactersInRange:NSMakeRange(0, tempString.length)];
            }
            
        }else{
            [tempString appendString:[string substringFromIndex:8]];
        }
    }
    
    return tempArray;
/*
    NSMutableArray *commonArray = [NSMutableArray array];
    for (NSString *message in tempArray) {
        if ([[message substringWithRange:NSMakeRange(0, 3)] isEqualToString:@"REC"]) {
            [commonArray addObject:@{@"songName":[message substringWithRange:NSMakeRange(3, 4)],@"singerName":@"录音文件"}];
        }else{
            NSArray *array = [message componentsSeparatedByString:@" "];
            if (array.count > 2) {
                NSString *musicName = [array lastObject];
                if (musicName.length > 4) {
                    musicName = [musicName substringToIndex:musicName.length-4];
                    NSString *singerName = [array firstObject];
                    if (singerName.length > 0 ) {
                        [commonArray addObject:@{@"songName":musicName,@"singerName":singerName}];
                        
                    }
                }
                
            }
        }
    }
//    [self featchBlueSongToSave:commonArray];
//    [self featchBlueSongToSave:commonArray];
    return commonArray;
*/
}
//-(void)featchBlueSongToSave:(NSArray *)array{
//    PlayLocalMusicListManager *manager = [[PlayLocalMusicListManager alloc]init];
//    for (NSDictionary *dict in array) {
////        NSArray *array = [message componentsSeparatedByString:@" "];
//        NSString *musicName = dict[@"songName"];
////        musicName = [musicName substringToIndex:musicName.length-4];
//        NSString *singerName = dict[@"singerName"];
//
//        if (![manager judeUserSaveFormName:musicName]) {
////            [manager featchMusicLyricWithKeyword:singerName singerName:musicName];
//            [manager featchMusicLyricWithKeyword:musicName singerName:singerName];
//            NSLog(@"abcdefg:%@",musicName);
//        }
//        if (![manager judeUserSaveFormName:singerName]) {
//            [manager featchMusicLyricWithKeyword:singerName singerName:musicName];
//            NSLog(@"abcdefg:%@",singerName);
//
////            [manager featchMusicLyricWithKeyword:musicName singerName:singerName];
//        }
//    }
////    NSLog(@"歌名列表：%@",[MBOXManager shareInstance].FFF1MusicDataNameArray);
//}

-(NSInteger)deleteZeroFromString:(NSString *)string{
    BOOL isZero = NO;
    NSInteger zeroLocation = 0;
    for (NSInteger i = 0; i < string.length; i ++) {
        NSString *singleString = [string substringWithRange:NSMakeRange(i, 1)];
        if (isZero == NO && [singleString isEqualToString:@"0"]) {
            isZero = YES;
            zeroLocation = i;
        }
        if (![singleString isEqualToString:@"0"]) {
            isZero = NO;
        }
    }
    return zeroLocation;
}

-(NSString *)StringFomHexString:(NSString *)string{
//    NSString *mp3String = @"0033";
//    NSRange rang = [string rangeOfString:mp3String];
////    if (rang.length > 0) {
////        return nil;
////    }
//    NSString *frontString = [string substringToIndex:rang.location];
    NSString*command = [string substringToIndex:[self deleteZeroFromString:string]];
    NSInteger yu = command.length %4;
    if (yu % 4 > 0) {
        for (NSInteger i = 0; i < 4 - yu; i ++) {
            command = [NSString stringWithFormat:@"%@0",command];
        }
    }
    //1位倒转
    NSMutableString *tempComandString = [[NSMutableString alloc]init];
    for (NSInteger i = 0; i < command.length; i = i+2) {
        NSString *subString = [command substringWithRange:NSMakeRange(i, 2)];
        NSMutableString *newString = [[NSMutableString alloc]init];
        for (NSInteger i = subString.length - 1; i >= 0; i --) {
            [newString appendString:[subString substringWithRange:NSMakeRange(i, 1)]];
        }
        [tempComandString appendString:newString];
    }
    command = [NSString stringWithFormat:@"%@",tempComandString];
    //每两位倒转
    NSMutableString *mewTempString = [NSMutableString string];
    for (NSInteger i = 0; i < command.length; i = i+4) {
        NSString *subString = [command substringWithRange:NSMakeRange(i, 4)];
        NSMutableString *newString = [[NSMutableString alloc]init];
        for (NSInteger i = subString.length - 1; i >= 0; i --) {
            [newString appendString:[subString substringWithRange:NSMakeRange(i, 1)]];
        }
        [mewTempString appendString:newString];
    }
    command = [NSString stringWithFormat:@"%@",mewTempString];
    command =[command stringByReplacingOccurrencesOfString:@" "withString:@""];
    
    NSMutableData *commandToSend= [[NSMutableData alloc] init];
    
    unsigned char whole_byte;
    
    char byte_chars[3] = {'\0','\0','\0'};
    
    int i;
    
    for (i=0; i< [command length]/2; i++) {
        
        byte_chars[0] = [command characterAtIndex:i*2];
        
        byte_chars[1] = [command characterAtIndex:i*2+1];
        
        whole_byte= strtol(byte_chars, NULL, 16);
        [commandToSend appendBytes:&whole_byte length:1];
    }
    NSLog(@"commandToSend:%@",commandToSend);
    
    NSString *string11 = [[NSString alloc]initWithData:commandToSend encoding:NSUnicodeStringEncoding];
    
    NSLog(@"newString:%@",string11);
    return string11;
}
@end
